<?php
/**
 * Manage course transaction in admin
 *
 */
class CourseTransactionController extends AppController {
	var $uses = array('Transaction');
	var $name = 'CourseTransaction';

	/**
	 * List course detail with summary transaction
	 */
	function admin_index() {
		$this->loadModel('CourseDetail');
		$options = $this->CourseDetail->getPaginateOption();
		if (isset($this->params['url']['type']) && $this->params['url']['type'] == 'users') {
			$this->redirect(array('controller' => 'UserTransactions', 'action' => 'index'));
		}
		if (isset($this->params['url']['limit']) && $limit = $this->params['url']['limit']) {
			$options['limit'] = intval($limit);
		} else {
			$options['limit'] = 25;
		}
		if (isset($this->params['url']['title']) && $title = $this->params['url']['title']) {
			$options['conditions']['Course.title LIKE'] = '%'.$title.'%';
		}
		if (isset($this->params['url']['course']) && ($course = $this->params['url']['course']) !== '') {
			if ($course === '0') {
				$options['conditions']['CourseDetail.del_flg'] = 0;
			} else {
				$options['conditions']['CourseDetail.del_flg'] = 1;
			}
		} else {
			if (isset($options['conditions']['Course.del_flg'])) {
				unset($options['conditions']['CourseDetail.del_flg']);
			}
		}
		if (isset($this->params['url']['payout']) && ($payout = $this->params['url']['payout']) !== '') {
			if ($payout === '0') {
				$options['conditions']['CourseDetail.payout_flg'] = 0;
			} else {
				$options['conditions']['CourseDetail.payout_flg'] = 1;
			}
		}
		$this->paginate = $options;
		$details = $this->paginate('CourseDetail');
		$this->set(array(
			'details' => $details,
		));
	}

	/**
	 * List transaction of course detail
	 *
	 * @param null $id Id of course detail
	 */
	function admin_view($id = null) {
		$this->loadModel('CourseDetail');
		$courseDetail = $this->CourseDetail->getCourseDetail($id);
		if (!$courseDetail) {
			$this->cakeError('error404');
		}
		$options = array();
		$options = $this->Transaction->getTransactionOfDetail($id, true);
		if (isset($this->params['url']['limit']) && $limit = $this->params['url']['limit']) {
			$options['limit'] = intval($limit);
		} else {
			$options['limit'] = 25;
		}
		if (isset($this->params['url']['payment']) && ($payment = $this->params['url']['payment']) !== '') {
			if ($payment === '0') {
				$options['conditions']['Transaction.status'] = 0;
			} else {
				$options['conditions']['Transaction.status'] = 1;
			}
		} else {
			if (isset($options['conditions']['Transaction.status'])) {
				unset($options['conditions']['Transaction.status']);
			}
		}
		if (isset($this->params['url']['delete']) && ($delete = $this->params['url']['delete']) !== '') {
			if ($delete === '0') {
				$options['conditions']['Transaction.del_flg'] = 0;
			} else {
				$options['conditions']['Transaction.del_flg'] = 1;
			}
		} else {
			if (isset($options['conditions']['Transaction.del_flg'])) {
				unset($options['conditions']['Transaction.del_flg']);
			}
		}
		$this->paginate = $options;
		$transactions = $this->paginate('Transaction');
		$this->set(array(
			'courseDetail' => $courseDetail,
			'transactions' => $transactions,
		));
	}
}